package com.lej.core.dsl

import android.R
import android.content.Context
import android.content.res.ColorStateList
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.Typeface
import android.util.TypedValue
import android.view.*
import android.widget.*
import androidx.annotation.ColorInt
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintHelper
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.Guideline
import androidx.core.content.res.ResourcesCompat
import androidx.core.view.MarginLayoutParamsCompat
import androidx.core.view.updateLayoutParams
import androidx.core.widget.NestedScrollView
import androidx.fragment.app.Fragment
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.recyclerview.widget.RecyclerView
import com.qmuiteam.qmui.layout.QMUIButton
import com.qmuiteam.qmui.layout.QMUIFrameLayout
import com.qmuiteam.qmui.widget.QMUIRadiusImageView

//<editor-fold desc="widget creation function">
inline fun ViewGroup.TextView(init: TextView.() -> Unit) =
    TextView(context).apply(init).also { addView(it) }

inline fun ViewGroup.Space(init: Space.() -> Unit) =
    Space(context).apply(init).also { addView(it) }

inline fun ViewGroup.ImageView(init: ImageView.() -> Unit) =
    ImageView(context).apply(init).also { addView(it) }

inline fun ViewGroup.QMUIButton(init: QMUIButton.() -> Unit) =
    QMUIButton(context).apply(init).also { addView(it) }

inline fun ViewGroup.Button(init: Button.() -> Unit) =
    Button(context).apply(init).also { addView(it) }

inline fun ViewGroup.View(init: View.() -> Unit): View =
    View(context).apply(init).also { addView(it) }

inline fun ViewGroup.RadioButton(init: RadioButton.() -> Unit): View =
    RadioButton(context).apply(init).also { addView(it) }

inline fun ViewGroup.CheckBox(init: CheckBox.() -> Unit): View =
    CheckBox(context).apply(init).also { addView(it) }

inline fun ViewGroup.RelativeLayout(init: RelativeLayout.() -> Unit) =
    RelativeLayout(context).apply(init).also { addView(it) }

inline fun ViewGroup.LinearLayout(init: LinearLayout.() -> Unit) =
    LinearLayout(context).apply(init).also { addView(it) }

inline fun ViewGroup.NestedScrollView(init: NestedScrollView.() -> Unit) =
    NestedScrollView(context).apply(init).also { addView(it) }

inline fun ViewGroup.RecyclerView(init: RecyclerView.() -> Unit) =
    RecyclerView(context).apply(init).also { addView(it) }

inline fun ViewGroup.ConstraintLayout(init: ConstraintLayout.() -> Unit) =
    ConstraintLayout(context).apply(init).also { addView(it) }

inline fun ViewGroup.FrameLayout(init: FrameLayout.() -> Unit) =
    FrameLayout(context).apply(init).also { addView(it) }

inline fun ViewGroup.ViewFlipper(init: ViewFlipper.() -> Unit) =
    ViewFlipper(context).apply(init).also { addView(it) }

inline fun ConstraintLayout.Guideline(init: Guideline.() -> Unit) =
    Guideline(context).apply(init).also { addView(it) }

//inline fun ConstraintLayout.Flow(init: Flow.() -> Unit) =
//    Flow(context).apply(init).also { addView(it) }
//
//inline fun ConstraintLayout.Layer(init: Layer.() -> Unit) =
//    Layer(context).apply(init).also { addView(it) }

inline fun Context.ConstraintLayout(init: ConstraintLayout.() -> Unit): ConstraintLayout =
    ConstraintLayout(this).apply(init)

inline fun Context.LinearLayout(init: LinearLayout.() -> Unit): LinearLayout =
    LinearLayout(this).apply(init)

inline fun Context.FrameLayout(init: FrameLayout.() -> Unit) =
    FrameLayout(this).apply(init)

inline fun Context.NestedScrollView(init: NestedScrollView.() -> Unit) =
    NestedScrollView(this).apply(init)

inline fun Context.TextView(init: TextView.() -> Unit) =
    TextView(this).apply(init)

inline fun Context.Button(init: Button.() -> Unit) =
    Button(this).apply(init)

inline fun Context.ImageView(init: ImageView.() -> Unit) =
    ImageView(this).apply(init)

inline fun Context.View(init: View.() -> Unit) =
    View(this).apply(init)

inline fun Context.ViewFlipper(init: ViewFlipper.() -> Unit) =
    ViewFlipper(this).apply(init)

inline fun Fragment.ConstraintLayout(init: ConstraintLayout.() -> Unit) =
    context?.let { ConstraintLayout(it).apply(init) }

inline fun Fragment.LinearLayout(init: LinearLayout.() -> Unit) =
    context?.let { LinearLayout(it).apply(init) }

inline fun Fragment.FrameLayout(init: FrameLayout.() -> Unit) =
    context?.let { FrameLayout(it).apply(init) }

inline fun Fragment.NestedScrollView(init: NestedScrollView.() -> Unit) =
    context?.let { NestedScrollView(it).apply(init) }

inline fun Fragment.TextView(init: TextView.() -> Unit) =
    context?.let { TextView(it).apply(init) }

inline fun Fragment.Button(init: Button.() -> Unit) =
    context?.let { Button(it).apply(init) }

inline fun Fragment.ImageView(init: ImageView.() -> Unit) =
    context?.let { ImageView(it).apply(init) }

inline fun Fragment.View(init: View.() -> Unit) =
    context?.let { View(it).apply(init) }

inline fun Fragment.ViewFlipper(init: ViewFlipper.() -> Unit) =
    context?.let { ViewFlipper(it).apply(init) }
//</editor-fold>

//<editor-fold desc="View extend field">
inline var View.layout_id: String
    get() {
        return ""
    }
    set(value) {
        id = value.toLayoutId()
    }
inline var View.padding_top: Int
    get() {
        return 0
    }
    set(value) {
        setPadding(paddingLeft, value.dp, paddingRight, paddingBottom)
    }

inline var View.padding_bottom: Int
    get() {
        return 0
    }
    set(value) {
        setPadding(paddingLeft, paddingTop, paddingRight, value.dp)
    }
inline var View.padding_vertical: Int
    get() {
        return 0
    }
    set(value) {
        setPadding(paddingLeft, value.dp, paddingRight, value.dp)
    }
inline var View.padding_horizontal: Int
    get() {
        return 0
    }
    set(value) {
        setPadding(value.dp, paddingTop, value.dp, paddingBottom)
    }

inline var View.padding_start: Int
    get() {
        return 0
    }
    set(value) {
        setPadding(value.dp, paddingTop, paddingRight, paddingBottom)
    }

inline var View.padding_end: Int
    get() {
        return 0
    }
    set(value) {
        setPadding(paddingLeft, paddingTop, value.dp, paddingBottom)
    }

inline var View.padding_end_px: Int
    get() {
        return 0
    }
    set(value) {
        setPadding(paddingLeft, paddingTop, value, paddingBottom)
    }
inline var View.padding: Int
    get() {
        return 0
    }
    set(value) {
        setPadding(value.dp, value.dp, value.dp, value.dp)
    }
inline var View.layout_width: Int
    get() {
        return 0
    }
    set(value) {
        val pxValue = if (value > 0) value.dp else value
        layoutParams = layoutParams?.apply {
            width = pxValue
        } ?: ViewGroup.MarginLayoutParams(pxValue, match_parent)
    }

inline var View.layout_height: Int
    get() {
        return 0
    }
    set(value) {
        val pxValue = if (value > 0) value.dp else value
        layoutParams = layoutParams?.apply {
            height = pxValue
        } ?: ViewGroup.MarginLayoutParams(match_parent, pxValue)
    }

inline var View.alignParentStart: Boolean
    get() {
        return false
    }
    set(value) {
        if (!value) return
        layoutParams = RelativeLayout.LayoutParams(layoutParams).apply {
            (layoutParams as? RelativeLayout.LayoutParams)?.rules?.forEachIndexed { index, i ->
                addRule(index, i)
            }
            addRule(RelativeLayout.ALIGN_PARENT_START, RelativeLayout.TRUE)
        }
    }

inline var View.alignParentEnd: Boolean
    get() {
        return false
    }
    set(value) {
        if (!value) return
        layoutParams = RelativeLayout.LayoutParams(layoutParams).apply {
            (layoutParams as? RelativeLayout.LayoutParams)?.rules?.forEachIndexed { index, i ->
                addRule(index, i)
            }
            addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE)
        }
    }

inline var View.centerVertical: Boolean
    get() {
        return false
    }
    set(value) {
        if (!value) return
        layoutParams = RelativeLayout.LayoutParams(layoutParams).apply {
            (layoutParams as? RelativeLayout.LayoutParams)?.rules?.forEachIndexed { index, i ->
                addRule(index, i)
            }
            addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE)
        }
    }

inline var View.centerInParent: Boolean
    get() {
        return false
    }
    set(value) {
        if (!value) return
        layoutParams = RelativeLayout.LayoutParams(layoutParams).apply {
            (layoutParams as? RelativeLayout.LayoutParams)?.rules?.forEachIndexed { index, i ->
                addRule(index, i)
            }
            addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE)
        }
    }

inline var View.weight: Float
    get() {
        return 0f
    }
    set(value) {
        layoutParams = LinearLayout.LayoutParams(layoutParams).also { it ->
            it.gravity = (layoutParams as? LinearLayout.LayoutParams)?.gravity ?: -1
            it.weight = value
        }
    }

@Deprecated(message = "这个方法只适合父布局是LineaLayout")
inline var View.layout_gravity: Int
    get() {
        return -1
    }
    set(value) {
        when (val it = layoutParams) {
            is LinearLayout.LayoutParams -> {
                LinearLayout.LayoutParams(it).apply {
                    weight = (layoutParams as? LinearLayout.LayoutParams)?.weight ?: 0f
                    gravity = value
                }
            }
            is FrameLayout.LayoutParams -> FrameLayout.LayoutParams(it).apply { gravity = value }
            else -> {
                layoutParams = LinearLayout.LayoutParams(wrap_content, wrap_content)
                    .apply { gravity = value }
            }
        }
    }

inline var View.start_toStartOf: String
    get() {
        return ""
    }
    set(value) {
        layoutParams = layoutParams.append {
            startToStart = value.toLayoutId()
            startToEnd = -1
        }
    }

inline var View.start_toEndOf: String
    get() {
        return ""
    }
    set(value) {
        layoutParams = layoutParams.append {
            startToEnd = value.toLayoutId()
            startToStart = -1
        }
    }

inline var View.top_toBottomOf: String
    get() {
        return ""
    }
    set(value) {
        layoutParams = layoutParams.append {
            topToBottom = value.toLayoutId()
            topToTop = -1
        }
    }

inline var View.top_toTopOf: String
    get() {
        return ""
    }
    set(value) {
        layoutParams = layoutParams.append {
            topToTop = value.toLayoutId()
            topToBottom = -1
        }
    }

inline var View.end_toEndOf: String
    get() {
        return ""
    }
    set(value) {
        layoutParams = layoutParams.append {
            endToEnd = value.toLayoutId()
            endToStart = -1
        }
    }

inline var View.end_toStartOf: String
    get() {
        return ""
    }
    set(value) {
        layoutParams = layoutParams.append {
            endToStart = value.toLayoutId()
            endToEnd = -1
        }
    }

inline var View.bottom_toBottomOf: String
    get() {
        return ""
    }
    set(value) {
        layoutParams = layoutParams.append {
            bottomToBottom = value.toLayoutId()
            bottomToTop = -1
        }
    }

inline var View.bottom_toTopOf: String
    get() {
        return ""
    }
    set(value) {
        layoutParams = layoutParams.append {
            bottomToTop = value.toLayoutId()
            bottomToBottom = -1
        }
    }
inline var View.circleTo: String
    get() {
        return ""
    }
    set(value) {
        layoutParams = layoutParams.append {
            circleConstraint = value.toLayoutId()
        }
    }
inline var View.circleRadius: Int
    get() {
        return (layoutParams as? ConstraintLayout.LayoutParams)?.circleRadius ?: 0
    }
    set(value) {
        layoutParams = layoutParams.append {
            circleRadius = value.dp
        }
    }
inline var View.circleAngle: Float
    get() {
        return (layoutParams as? ConstraintLayout.LayoutParams)?.circleAngle ?: 0f
    }
    set(value) {
        layoutParams = layoutParams.append {
            circleAngle = value
        }
    }
inline var View.horizontal_chain_style: Int
    get() {
        return -1
    }
    set(value) {
        layoutParams = layoutParams.append {
            horizontalChainStyle = value
        }
    }

inline var View.vertical_chain_style: Int
    get() {
        return -1
    }
    set(value) {
        layoutParams = layoutParams.append {
            verticalChainStyle = value
        }
    }

inline var View.horizontal_bias: Float
    get() {
        return -1f
    }
    set(value) {
        layoutParams = layoutParams.append {
            horizontalBias = value
        }
    }
inline var View.vertical_bias: Float
    get() {
        return -1f
    }
    set(value) {
        layoutParams = layoutParams.append {
            verticalBias = value
        }
    }

inline var View.center_horizontal: Boolean
    get() {
        return false
    }
    set(value) {
        if (!value) return
        start_toStartOf = parent_id
        end_toEndOf = parent_id
    }

inline var View.center_vertical: Boolean
    get() {
        return false
    }
    set(value) {
        if (!value) return
        top_toTopOf = parent_id
        bottom_toBottomOf = parent_id
    }

inline var View.align_vertical_center: String
    get() {
        return ""
    }
    set(value) {
        top_toTopOf = value
        bottom_toBottomOf = value
    }

inline var View.align_horizontal_center: String
    get() {
        return ""
    }
    set(value) {
        start_toStartOf = value
        end_toEndOf = value
    }

inline var View.background_color: String
    get() {
        return ""
    }
    set(value) {
        setBackgroundColor(Color.parseColor(value))
    }

inline var View.background_res: Int
    get() {
        return -1
    }
    set(value) {
        setBackgroundResource(value)
    }

inline var View.margin_top: Int
    get() {
        return -1
    }
    set(value) {
        updateLayoutParams<ViewGroup.MarginLayoutParams> { topMargin = value.dp }
    }

inline var View.margin_top_px: Int
    get() {
        return -1
    }
    set(value) {
        updateLayoutParams<ViewGroup.MarginLayoutParams> { topMargin = value }
    }

inline var View.margin_bottom: Int
    get() {
        return -1
    }
    set(value) {
        (layoutParams as? ViewGroup.MarginLayoutParams)?.apply {
            bottomMargin = value.dp
        }
    }

inline var View.margin_start: Int
    get() {
        return -1
    }
    set(value) {
        (layoutParams as? ViewGroup.MarginLayoutParams)?.apply {
            MarginLayoutParamsCompat.setMarginStart(this, value.dp)
        }
    }

inline var View.margin_end: Int
    get() {
        return -1
    }
    set(value) {
        (layoutParams as? ViewGroup.MarginLayoutParams)?.apply {
            MarginLayoutParamsCompat.setMarginEnd(this, value.dp)
        }
    }

inline var View.layout_visibility: Int
    get() {
        return -1
    }
    set(value) {
        visibility = value
    }

inline var View.bindVisibility: LiveData<Int>?
    get() {
        return null
    }
    set(value) {
        observe(value) { visibility = it }
    }

inline var View.bindBackgroundColor: LiveData<String>?
    get() {
        return null
    }
    set(value) {
        observe(value) {
            background_color = it
        }
    }

inline var View.bindBackgroundRes: LiveData<Int>?
    get() {
        return null
    }
    set(value) {
        observe(value) {
            background_res = it
        }
    }

inline var ImageView.src: Int
    get() {
        return -1
    }
    set(value) {
        setImageResource(value)
    }

inline var ImageView.tintColor: Int
    get() {
        return -1
    }
    set(value) {
        imageTintList = ColorStateList.valueOf(value)
    }

inline var ImageView.bindSrc: LiveData<Bitmap>?
    get() {
        return null
    }
    set(value) {
        observe(value) { setImageBitmap(it) }
    }

inline var ImageView.bindSrcRes: LiveData<Int>?
    get() {
        return null
    }
    set(value) {
        observe(value) { setImageResource(it) }
    }

inline var TextView.bindText: LiveData<CharSequence>?
    get() {
        return null
    }
    set(value) {
        observe(value) { text = it }
    }
//inline var TextView.bindTextColor: LiveData<String>?
//    get() {
//        return null
//    }
//    set(value) {
//        observe(value) {
//            textColor = it
//        }
//    }

inline var TextView.textRes: Int
    get() {
        return -1
    }
    set(value) {
        setText(value)
    }

inline var TextView.textStyle: Int
    get() {
        return -1
    }
    set(value) = setTypeface(typeface, value)


inline var TextView.bold: Boolean
    get() {
        return false
    }
    set(value) {
        paint.isFakeBoldText = value
        postInvalidate()
    }

/**设置宽高比 by ConstraintLayout*/
inline fun View.dimensionRatio(ratio: String) {
    (layoutParams as? ConstraintLayout.LayoutParams)?.dimensionRatio = ratio
}


inline var TextView.textColor: Int
    @ColorInt
    get() {
        return textColors.defaultColor
    }
    set(value) {
        setTextColor(value)
    }

inline var TextView.fontFamily: Int
    get() {
        return 1
    }
    set(value) {
        typeface = ResourcesCompat.getFont(context, value)
    }

inline var Button.textAllCaps: Boolean
    get() {
        return false
    }
    set(value) {
        isAllCaps = value
    }


inline var NestedScrollView.fadeScrollBar: Boolean
    get() {
        return false
    }
    set(value) {
        isScrollbarFadingEnabled = value
    }

inline var ConstraintHelper.referenceIds: String
    get() {
        return ""
    }
    set(value) {
        referencedIds = value.split(",").map { it.toLayoutId() }.toIntArray()
    }

//inline var Flow.flow_horizontalGap: Int
//    get() {
//        return 0
//    }
//    set(value) {
//        setHorizontalGap(value.dp)
//    }
//
//inline var Flow.flow_verticalGap: Int
//    get() {
//        return 0
//    }
//    set(value) {
//        setVerticalGap(value.dp)
//    }
//
//inline var Flow.flow_wrapMode: Int
//    get() {
//        return 0
//    }
//    set(value) {
//        setWrapMode(value)
//    }

var View.onClick: (View) -> Unit
    get() {
        return {}
    }
    set(value) {
        setOnClickListener { v -> value(v) }
    }

var RecyclerView.onItemClick: (View, Int) -> Unit
    get() {
        return { _, _ -> }
    }
    set(value) {
        setOnItemClickListener(value)
    }
//</editor-fold>


//<editor-fold desc="View layout constant">
val match_parent = ViewGroup.LayoutParams.MATCH_PARENT
val wrap_content = ViewGroup.LayoutParams.WRAP_CONTENT

val visible = View.VISIBLE
val gone = View.GONE
val invisible = View.INVISIBLE

val horizontal = LinearLayout.HORIZONTAL
val vertical = LinearLayout.VERTICAL

val bold = Typeface.BOLD
val normal = Typeface.NORMAL
val italic = Typeface.ITALIC
val bold_italic = Typeface.BOLD_ITALIC

val gravity_center = Gravity.CENTER
val gravity_left = Gravity.LEFT
val gravity_right = Gravity.RIGHT
val gravity_bottom = Gravity.BOTTOM
val gravity_top = Gravity.TOP
val gravity_center_horizontal = Gravity.CENTER_HORIZONTAL
val gravity_center_vertical = Gravity.CENTER_VERTICAL

val scale_fix_xy = ImageView.ScaleType.FIT_XY
val scale_center_crop = ImageView.ScaleType.CENTER_CROP
val scale_center = ImageView.ScaleType.CENTER
val scale_center_inside = ImageView.ScaleType.CENTER_INSIDE
val scale_fit_center = ImageView.ScaleType.FIT_CENTER
val scale_fit_end = ImageView.ScaleType.FIT_END
val scale_matrix = ImageView.ScaleType.MATRIX
val scale_fit_start = ImageView.ScaleType.FIT_START

//val constraint_start = ConstraintProperties.START
//val constraint_end = ConstraintProperties.END
//val constraint_top = ConstraintProperties.TOP
//val constraint_bottom = ConstraintProperties.BOTTOM
//val constraint_baseline = ConstraintProperties.BASELINE
//val constraint_parent = ConstraintProperties.PARENT_ID

val spread = ConstraintLayout.LayoutParams.CHAIN_SPREAD
val packed = ConstraintLayout.LayoutParams.CHAIN_PACKED
val spread_inside = ConstraintLayout.LayoutParams.CHAIN_SPREAD_INSIDE

//val wrap_none = Flow.WRAP_NONE
//val wrap_chain = Flow.WRAP_CHAIN
//val wrap_aligned = Flow.WRAP_ALIGNED

val parent_id = "0"
//</editor-fold>

//<editor-fold desc="layout helper function">
val Int.dp: Int
    get() {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            this.toFloat(),
            Resources.getSystem().displayMetrics
        ).toInt()
    }

val Float.dp: Int
    get() {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            this,
            Resources.getSystem().displayMetrics
        ).toInt()
    }

val Int.sp: Int
    get() {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_SP,
            this.toFloat(),
            Resources.getSystem().displayMetrics
        ).toInt()
    }

fun TextView.stateEnableTextColor(norCol: Int, enableCol: Int) {
    val colors = intArrayOf(enableCol, norCol)
    val states = arrayOf(intArrayOf(R.attr.state_enabled), intArrayOf(-R.attr.state_enabled))
    val colorList = ColorStateList(states, colors)
    setTextColor(colorList)
}

fun ViewGroup.MarginLayoutParams.toConstraintLayoutParam() =
    ConstraintLayout.LayoutParams(width, height).also { it ->
        it.topMargin = this.topMargin
        it.bottomMargin = this.bottomMargin
        it.marginStart = this.marginStart
        it.marginEnd = this.marginEnd
    }

fun ViewGroup.LayoutParams.append(set: ConstraintLayout.LayoutParams.() -> Unit) =
    (this as? ConstraintLayout.LayoutParams)?.apply(set)
        ?: (this as? ViewGroup.MarginLayoutParams)?.toConstraintLayoutParam()?.apply(set)


fun String.toLayoutId(): Int {
    var id = java.lang.String(this).bytes.getStrId()
    if (id == 48) id = 0
    return id
}

private fun ByteArray.getStrId(): Int {
    var sum = 0
    for (element in this.indices) {
        sum += (element + 1) * this[element]
    }
    return sum
}

fun <T : View> View.find(id: String): T? = findViewById(id.toLayoutId())

fun <T : View> AppCompatActivity.find(id: String): T? = findViewById(id.toLayoutId())

/**避免在复用的时候添加观察者*/
fun <T> View.observe(liveData: LiveData<T>?, forever: Boolean = false, action: View.(T) -> Unit) {
    (context as? LifecycleOwner)?.let { owner ->
        if (forever) liveData?.observeForever { action(it) }
        else liveData?.observe(owner, { action(it) })
    }
}

inline fun <T : View> T.inEdit(action: T.() -> Unit) {
    if (isInEditMode) action()
}

fun RecyclerView.setOnItemClickListener(listener: (View, Int) -> Unit) {
    addOnItemTouchListener(object : RecyclerView.OnItemTouchListener {
        val gestureDetector = GestureDetector(context, object : GestureDetector.OnGestureListener {
            override fun onShowPress(e: MotionEvent?) {
            }

            override fun onSingleTapUp(e: MotionEvent?): Boolean {
                e?.let {
                    findChildViewUnder(it.x, it.y)?.let { child ->
                        listener(child, getChildAdapterPosition(child))
                    }
                }
                return false
            }

            override fun onDown(e: MotionEvent?): Boolean {
                return false
            }

            override fun onFling(
                e1: MotionEvent?,
                e2: MotionEvent?,
                velocityX: Float,
                velocityY: Float
            ): Boolean {
                return false
            }

            override fun onScroll(
                e1: MotionEvent?,
                e2: MotionEvent?,
                distanceX: Float,
                distanceY: Float
            ): Boolean {
                return false
            }

            override fun onLongPress(e: MotionEvent?) {
            }
        })

        override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {

        }

        override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
            gestureDetector.onTouchEvent(e)
            return false
        }

        override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {
        }
    })
}
//</editor-fold>


////custom


inline fun ViewGroup.QMUIRadiusImageView(init: QMUIRadiusImageView.() -> Unit) =
    QMUIRadiusImageView(context).apply(init).also { addView(it) }

inline fun ViewGroup.QMUIFrameLayout(init: QMUIFrameLayout.() -> Unit) =
    QMUIFrameLayout(context).apply(init).also { addView(it) }
